package filter

import (
	"file_explorer/model"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/spf13/cast"
)

type RecursionFilter struct {
	content *fyne.Container
	entry   *widget.Entry
}

func (filter *RecursionFilter) GetCanvasObject() fyne.CanvasObject {
	return filter.content
}

func (filter *RecursionFilter) Filter(fileInfo *model.FileInfo) bool {
	return true
}

func (filter *RecursionFilter) GetValue() string {
	return filter.entry.Text
}

func (filter *RecursionFilter) LoadValue(val string) {
	filter.entry.Text = val
	filter.entry.Refresh()
}

func (filter *RecursionFilter) Reset() {
	filter.LoadValue("")
}

func (filter *RecursionFilter) GetDepth() int {
	return cast.ToInt(filter.entry.Text)
}

func NewRecursionFilter(onChange func()) *RecursionFilter {
	filter := &RecursionFilter{
		entry: widget.NewEntry(),
	}

	filter.entry.PlaceHolder = "depth"

	filter.content = container.NewBorder(nil, nil,
		widget.NewButton("-", func() {
			depth := cast.ToInt(filter.entry.Text) - 1
			if depth < 0 {
				depth = 0
			}
			filter.entry.SetText(cast.ToString(depth))
		}),
		widget.NewButton("+", func() {
			filter.entry.SetText(cast.ToString(cast.ToInt(filter.entry.Text) + 1))

		}),
		filter.entry,
	)
	filter.entry.OnChanged = func(s string) {
		onChange()
	}

	return filter
}
